import os
import logging
import jieba
import re
import math
from collections import Counter
import numpy as np

logger = logging.getLogger(__name__)

class ContentQualityAnalyzer:
    def __init__(self):
        self.stop_words = self._load_stopwords()
    
    def _load_stopwords(self):
        """加载停用词典"""
        try:
            # 创建默认词典目录
            quality_dir = os.path.join('data', 'quality')
            os.makedirs(quality_dir, exist_ok=True)
            
            stopwords_path = os.path.join(quality_dir, 'stopwords.txt')
            if os.path.exists(stopwords_path):
                with open(stopwords_path, 'r', encoding='utf-8') as f:
                    return set(line.strip() for line in f if line.strip())
            else:
                # 默认停用词列表
                default_stopwords = self._get_default_stopwords()
                
                # 写入默认停用词
                with open(stopwords_path, 'w', encoding='utf-8') as f:
                    for word in default_stopwords:
                        f.write(f"{word}\n")
                
                return default_stopwords
        except Exception as e:
            logger.error(f"加载停用词失败: {str(e)}")
            return self._get_default_stopwords()
    
    def _get_default_stopwords(self):
        """获取默认停用词"""
        return set(['的', '了', '在', '是', '我', '有', '和', '就', '不', '人', '都', '一', '一个', '上', '也', '很', 
                  '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这', '那', '啊', '呢',
                  '吧', '吗', '嗯', '哦', '哪', '哼', '啦', '呀', '这个', '那个', '怎么', '什么'])
    
    def analyze(self, text):
        """分析内容质量，返回质量得分"""
        if not text or len(text) < 10:
            return {"quality_score": 0.3}
        
        try:
            # 文本基础统计
            chars = len(text)
            words = jieba.lcut(text)
            words = [w for w in words if w not in self.stop_words and len(w.strip()) > 0]
            
            # 计算词汇多样性
            unique_words = set(words)
            if not words:
                diversity = 0
            else:
                diversity = len(unique_words) / len(words)
            
            # 计算句子数量和平均长度
            sentences = re.split(r'[。！？.!?]', text)
            sentences = [s for s in sentences if s.strip()]
            avg_sentence_length = chars / len(sentences) if sentences else chars
            
            # 句子长度合理性（太长或太短都不好）
            sentence_length_score = 0
            if 10 <= avg_sentence_length <= 40:
                sentence_length_score = 1.0
            elif avg_sentence_length < 10:
                sentence_length_score = avg_sentence_length / 10
            else:
                sentence_length_score = max(0.2, 40 / avg_sentence_length)
            
            # 重复率检测（词频统计，排除停用词）
            word_counts = Counter(words)
            max_word_count = max(word_counts.values()) if word_counts else 0
            total_words = len(words)
            
            # 计算重复惩罚（最常见词占比越高，惩罚越大）
            if total_words > 10:
                repeat_ratio = max_word_count / total_words
                repetition_penalty = min(repeat_ratio * 2, 1.0)
            else:
                repetition_penalty = 0.2  # 短文本默认轻微惩罚
            
            # 计算句子结构分数
            sentence_structure_score = 0.5
            if len(sentences) >= 2:
                # 计算句子长度变化，变化越大越好
                lengths = [len(s) for s in sentences]
                length_variance = np.var(lengths) if len(lengths) > 1 else 0
                normalized_variance = min(length_variance / 100, 1.0)  # 标准化方差
                sentence_structure_score = 0.5 + (normalized_variance * 0.5)
            
            # 综合质量评分
            quality_score = (
                (0.3 * diversity) +              # 词汇多样性
                (0.3 * sentence_length_score) +  # 句子长度合理性
                (0.2 * sentence_structure_score) + # 句子结构多样性
                (0.2 * (1 - repetition_penalty))   # 惩罚重复
            )
            
            return {"quality_score": min(max(quality_score, 0), 1)}
        except Exception as e:
            logger.error(f"内容质量分析失败: {str(e)}")
            return {"quality_score": 0.5}  # 出错时返回中等分数

# 创建单例实例
quality_analyzer = ContentQualityAnalyzer() 